package com.example.grafstock_upload;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.csi.controle.upload.dados.Dados;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

public class ImageManager {

	private static List<Bitmap> imagens = new ArrayList<Bitmap>();

	public static final int REQUEST_IMAGE_CAPTURE = 1;
	
	public static final int REQUEST_IMAGE_GALERY = 2;
	
	private static ImageManager instancia;
	
	private GaleriaActivity galeriaActivity;
	
	private static String TAG = "ImagemManager";
	
	public static ImageManager getInstancia(Resources resources, GaleriaActivity galeriaActivity) {
		if(instancia == null) {
			instancia = new ImageManager(resources, galeriaActivity);
		}
		if(galeriaActivity != null) {
			instancia.galeriaActivity = galeriaActivity;
		}
		return instancia;
	}
	
	public ImageManager(Resources resources, GaleriaActivity galeriaActivity) {
		Bitmap icon = BitmapFactory.decodeResource(resources, R.drawable.add_image);
		imagens.add(icon);
		this.galeriaActivity = galeriaActivity;
	}
	
	public void addImagem(Bitmap imagem) {
		List<Bitmap> imagensAux = new ArrayList<Bitmap>();
		imagensAux.add(imagem);
		imagensAux.addAll(imagens);
		imagens = imagensAux;
	}
	
	public int getCount() {
		return imagens.size();
	}

	public List<Bitmap> getImagens() {
		return imagens;
	}

	@SuppressLint("NewApi")
	public Intent tratarRetorno(int requestCode, int resultCode, Intent data, Context context) {
		if (resultCode == Activity.RESULT_OK) {
			Bitmap imageBitmap = null;
			switch (requestCode) {
			case ImageManager.REQUEST_IMAGE_CAPTURE:
				try {
					imageBitmap = MediaStore.Images.Media.getBitmap(galeriaActivity.getContentResolver(), galeriaActivity.getSelectedImageUri());
					Dados.filesTemp.add(new File(galeriaActivity.getSelectedImageUri().getPath()));
					addImagem(imageBitmap);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				break;
			case ImageManager.REQUEST_IMAGE_GALERY:
				Uri photoUri = data.getData();
				if(photoUri != null) {
					try {
						imageBitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), photoUri);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}					
					try {
						addImagem(imageBitmap);
						gravarImagem(imageBitmap);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {					
					for (int i = 0; i < data.getClipData().getItemCount(); i++) {
						photoUri = data.getClipData().getItemAt(i).getUri();
						if (photoUri != null) {
							try {
								imageBitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), photoUri);
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
						try {
							addImagem(imageBitmap);
						} catch (Exception e) {
							e.printStackTrace();
						}						
					}
				}
				break;
			default:
				break;
			}
		}
		return new Intent(context, GaleriaActivity.class);
	}

	private void gravarImagem(Bitmap imageBitmap) throws FileNotFoundException,
			IOException {
		Log.d(TAG, "Gravando a imagem... ");
		File pasta = new File(Environment.getExternalStorageDirectory() + File.separator + "controle-upload");
		if(!pasta.exists()) {				    	
			pasta.mkdir();
		}
		File file = new File(pasta, System.currentTimeMillis()+".jpg");
		FileOutputStream fos = new FileOutputStream(file);
		imageBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
		fos.flush();
		fos.close();
		Dados.filesTemp.add(file);
		Log.d(TAG, "Imagem salva: "+file.getPath());
	}
	
}
